<?php
#支付接口demo
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/6/17
 * Time: 11:50
 */
namespace app\applet\controller;

use WeixinPay\WeixinPay;

class WeChat
{
    /**
     * 小程序支付后的回调
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function notify()
    {
        $xml = file_get_contents('php://input'); //接受通知参数；
        $result = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        if($result)
        {
            //如果成功返回了
            $out_trade_no = $result['out_trade_no'];
            $trade_type   = $result['trade_type'];  //判断是否是jsapi支付
            $real_money = $result['total_fee'] / 100;
            if($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS')
            {
               #业务逻辑
                echo 'SUCCESS';
            } else {
                echo 'FAIL';
            }
        }
    }
    /**
     * 小程序支付
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function pay() {

        $openid = "oXBDV5BmMcJ6JBCZcSm2E2kkKiqM"; #用户openid
        $body = "测试订单"; #订单描述
        $total_fee = "100";#支付金额
        $out_trade_no = "20200617163012372137123";#订单编号
        // 实例微信支付基类
        $weixinPay = new WeixinPay($openid, $body, $total_fee, $out_trade_no);
        // 发起微信支付
        $result = $weixinPay->pay();
        dump($result);die;
    }



}